package com.founder.devwork.converter;

import com.founder.devwork.entity.CommitLog;
import com.founder.devwork.persistence.gateway.gitlab.dto.CommitDTO;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;

/**
 * GitCommitConverter 用于将 Git 提交信息转换为系统提交信息。
 * 提供从 CommitDTO 到 CommitLog 的转换方法。
 */
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface GitCommitConverter {
    /**
     * 将 Git 提交信息转换为系统提交信息。
     * @param dto Git 提交信息
     * @return 系统提交信息
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(source = "id", target = "sha")
    @Mapping(source = "repoId", target = "repositoryId")
    @Mapping(source = "authorName", target = "authorFullname")
    @Mapping(source = "committerName", target = "committerFullname")
    @Mapping(source = "authoredDate", target = "authoredDate", qualifiedByName = "toLocalDateTime")
    @Mapping(source = "committedDate", target = "committedDate", qualifiedByName = "toLocalDateTime")
    CommitLog gitCommitDtoToEntity(CommitDTO dto);

    /**
     * 将 Instant 转换为 LocalDateTime。
     * @param instant Instant 时间
     * @return LocalDateTime 时间
     */
    @Named("toLocalDateTime")
    default LocalDateTime toLocalDateTime(Instant instant) {
        return instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
}
